package cn.edu.csust.tysp.backend.common;

import com.alibaba.fastjson.annotation.JSONField;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Auther: snail
 * @Date: 2021/1/22 00:30
 * @Description: 分页查询基础响应对象
 */
@Data
public class ApiPageResult<T> extends ApiResult<T> {

    @ApiModelProperty(value = "总页数")
    @JSONField(ordinal = 5)
    private Integer totalPages;

    @ApiModelProperty(value = "总数")
    @JSONField(ordinal = 6)
    private Long totalResults;

    @ApiModelProperty(value = "每页记录数")
    @JSONField(ordinal = 7)
    private Integer pageSize;

    @ApiModelProperty(value = "当前页数")
    @JSONField(ordinal = 8)
    private Long currentPage;

    public ApiPageResult(Integer totalPages, Long totalResults, Integer pageSize, Long currentPage, T data) {
        super(data);
        this.totalPages = totalPages;
        this.totalResults = totalResults;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }

    public ApiPageResult() {
        super();
    }

    public static <T> ApiPageResult<T> ok(Integer totalPages, Long totalResults, Integer pageSize, Long currentPage, T data) {
        return new ApiPageResult(totalPages,totalResults,pageSize,currentPage,data);
    }

    public static ApiPageResult ok(){
        ApiPageResult result = new ApiPageResult();
        ApiResponse response = ApiResponse.ok();
        result.setCode(response.getCode());
        result.setMsg(response.getMsg());
        result.setSuccess(response.isSuccess());
        return result;
    }

    public static ApiPageResult error(String code, String msg) {
        ApiPageResult result = new ApiPageResult();
        result.setCode(code);
        result.setMsg(msg);
        result.setSuccess(Boolean.FALSE);
        return result;
    }
}